/**
 * @Author:Geln Yang
 * @Created:2008-8-7
 * $ Revision: 1.0 $
 * $ Author: Geln Yang $
 * $ Date: 2008-8-7 $
 */
package org.geln.web.webwork.convert;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import com.opensymphony.webwork.util.WebWorkTypeConverter;
import com.opensymphony.xwork.XworkException;

/**
 * @Author:Geln Yang
 * @Created:2008-8-7
 */
public class DateConvert extends WebWorkTypeConverter {
	private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
	
	public Object convertFromString(Map context, String[] values, Class toType) {
		Date  result = null;
		if(toType == Date.class){
			try {
				result = sdf.parse(values[0]);
			} catch (ParseException e) {
				throw new XworkException("Cound not parse date.",e);
			}
		}
		return result;
	}

	
	public String convertToString(Map context, Object o) {
		String result = null;
        if (o instanceof Date) {
            result = sdf.format(o);
        }
        return result;
	}

}
